<template>
	<view class="appointment-list">
		<!-- 导航栏 -->
		<nav-bar backUrl="/pages/my/my" :isTabbar="true"></nav-bar>
		<!-- 预约记录列表 -->
		<view class="record-list">
			<view v-if="appointmentList.length > 0">
				<!-- 预约记录项循环 -->
				<view 
					class="record-item" 
					v-for="(appointment, index) in appointmentList" 
					:key="index"
					:class="appointment.orderType === 1 ? 'store-visit' : 'home-service'"
				>
					<view class="record-title">{{appointment.orderType === 1 ? '预约到店' : '上门按摩'}}</view>
					<view class="record-content">
						<view class="info-row">
							<text class="label">订单号</text>
							<text class="value">{{appointment.orderNo}}</text>
						</view>
						<view class="info-row" v-if="appointment.orderType === 1">
							<text class="label">预约人</text>
							<text class="value">{{appointment.receiverName}}</text>
						</view>
						<template v-if="appointment.orderType === 1">
							<view class="info-row">
								<text class="label">预约门店</text>
								<text class="value">{{appointment.storeName}}</text>
							</view>
						</template>
						<view class="info-row">
							<text class="label">{{appointment.orderType === 1 ? '预约技师' : '服务技师'}}</text>
							<text class="value">{{appointment.technicianName}}</text>
						</view>
						<template v-if="appointment.orderType === 1">
							<view class="info-row">
								<text class="label">预约项目</text>
								<text class="value">{{appointment.serviceName}}</text>
							</view>
							<view class="info-row">
								<text class="label">门店地址</text>
								<text class="value">{{appointment.storeAddress}}</text>
							</view>
						</template>
						<template v-else>
							<view class="info-row">
								<text class="label">上门地址</text>
								<text class="value">{{appointment.addressDetail}}</text>
							</view>
						</template>
						<view class="info-row">
							<text class="label">预约时间</text>
							<text class="value">{{appointment.appointmentDate}} {{appointment.appointmentTime}}</text>
						</view>
					</view>
				</view>
			</view>
			
			<!-- 无数据提示 -->
			<view class="no-data" v-if="appointmentList.length === 0">
				<view class="empty-state">
					<image class="empty-icon" src="../../static/box.png" mode="aspectFit"></image>
					<text class="empty-text">暂无预约记录</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import NavBar from '@/components/nav-bar/nav-bar.vue'
export default {
	components: {
		NavBar
	},
	data() {
		return {
			appointmentList: []
		}
	},
	
	onLoad() {
		this.loadAppointments()
	},
	
	methods: {
		loadAppointments() {
			// 获取用户信息
			const userInfoStr = uni.getStorageSync('userInfo')
			let userInfo = null
			
			if (userInfoStr) {
			  try {
			    // 如果是字符串，尝试解析
			    if (typeof userInfoStr === 'string') {
			      const parsed = JSON.parse(userInfoStr)
			      // 检查是否是登录响应格式
			      if (parsed.code === 200 && parsed.data) {
			        userInfo = parsed.data
			      } else {
			        userInfo = parsed
			      }
			    } else {
			      // 如果不是字符串，直接使用
			      userInfo = userInfoStr
			    }
			  } catch (e) {
			    console.error('解析用户信息失败', e)
			    // 解析失败时，尝试直接使用
			    userInfo = userInfoStr
			  }
			}
			
			// 显示加载中
			uni.showLoading({
				title: '加载中...'
			})
			
			// 调用后端接口获取预约记录
			uni.request({
				url: `${uni.$config.baseUrl}/orders/orders/listAllHistory`,
				method: 'GET',
				data: {
					id: userInfo.memberId
				},
				success: (res) => {
					if (res.statusCode === 200 && res.data && res.data.code === 200) {
						this.appointmentList = res.data.rows || []
					} else {
						uni.showToast({
							title: res.data.msg || '获取预约记录失败',
							icon: 'none'
						})
					}
				},
				fail: (err) => {
					console.error('获取预约记录失败:', err)
					uni.showToast({
						title: '网络请求失败',
						icon: 'none'
					})
				},
				complete: () => {
					uni.hideLoading()
				}
			})
		},
		
		goBack() {
			uni.navigateBack()
		}
	}
}
</script>

<style lang="scss">
.appointment-list {
	min-height: 100vh;
	background: #F8F8F8;
	padding-bottom: 30rpx;
	
	.nav-header {
		background: #FFFFFF;
		padding: 20rpx 30rpx;
		display: flex;
		align-items: center;
		border-bottom: 1rpx solid #EEEEEE;
		
		.back-icon {
			font-size: 28rpx;
			color: #333;
			padding: 10rpx 20rpx;
		}
		
		.title {
			flex: 1;
			text-align: center;
			font-size: 32rpx;
			color: #333;
			font-weight: 500;
			margin-right: 60rpx; // 为了保持标题居中
		}
	}
	
	.record-list {
		padding: 20rpx;
		
		.record-item {
			background: #FFFFFF;
			border-radius: 12rpx;
			margin-bottom: 20rpx;
			overflow: hidden;
			
			.record-title {
				font-size: 32rpx;
				color: #333;
				font-weight: bold;
				padding: 20rpx 30rpx;
				border-bottom: 1rpx solid #EEEEEE;
			}
			
			.record-content {
				padding: 20rpx 30rpx;
				
				.info-row {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 16rpx;
					
					&:last-child {
						margin-bottom: 0;
					}
					
					.label {
						font-size: 28rpx;
						color: #666;
						min-width: 140rpx;
					}
					
					.value {
						flex: 1;
						font-size: 28rpx;
						color: #333;
						text-align: right;
					}
				}
			}
			
			&.store-visit {
				border-left: 6rpx solid #2B87FF;
			}
			
			&.home-service {
				border-left: 6rpx solid #FF6B6B;
			}
		}
		
		.no-data {
			display: flex;
			align-items: center;
			justify-content: center;
			min-height: 60vh;
			
			.empty-state {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				
				.empty-icon {
					width: 200rpx;
					height: 200rpx;
					margin-bottom: 30rpx;
				}
				
				.empty-text {
					font-size: 28rpx;
					color: #999999;
				}
			}
		}
	}
}
</style> 